﻿<?php
  
  include ('../raspored/common/config.php');
  $dsn = 'mysql:dbname='.$db_name.';host='.$server.';port=3306;charset=utf8';
try {
      $dbh = new PDO($dsn, $user, $pass);
}
catch (PDOException $e) {
      die('Konekcija neuspješna: ' . $e->getMessage(  ));
}

$dbh->query('SET CHARACTER SET utf8');
if ((($_FILES["photo-path"]["type"] != "image/gif")
&& ($_FILES["photo-path"]["type"] != "image/jpeg")
&& ($_FILES["photo-path"]["type"] != "image/jpg")
&& ($_FILES["photo-path"]["type"] != "image/png")
&& ($_FILES["photo-path"]["size"] != 0)))  {
  	echo '{
    "success":false,
    "msg":"Slika treba biti u jpg, jpeg, gif ili png formatu."}';
  } 
 else {
  

	$opis = $_POST['Opis'];
	$id = $_POST['id'];
	$notes = $_POST['Biljeske'];
	$statusi = $_POST['radio2'];
	$gorivo = $_POST['radio1'];
	$reg = $_POST['Registracija'];

	move_uploaded_file($_FILES["photo-path"]["tmp_name"],
      "upload/" . $_FILES["photo-path"]["name"]);
	  
	$slika = $_FILES["photo-path"]["name"];
	
	

	if(strlen($slika) == 0)
	{
		$slika = "NULL";
	}
		else
	{
		$slika = "'$slika'";
	}
	
	if(strlen($id) == 0)
	{
		$id = "NULL";
	}
	

	
	if(strlen($notes) == 0)
	{
		$notes = "NULL";
	}
		else
	{
		$notes = "'$notes'";
	}
	
	if(strlen($reg) == 0)
	{
		$reg = "NULL";
	}
		else
	{
		$reg = "'$reg'";
	}
	

	$upit="CALL vozilo_ui('$id', '$opis', $slika, '$statusi', $notes, '$gorivo', $reg, @status);";
    $sth = $dbh->query($upit);
    if ($dbh->errorCode() <> '00000') {
      die("Error: ".implode(': ',$dbh->errorInfo(  ))."\n");
    }
	
		
  	echo '{
    "success":true,
    "msg":"PHP poruka: Podaci su uspješno spremljeni"}';
	
  }
?>